/**
 * Copyright (C), 2001-2031, www.bosssof.com.cn
 * FileName: RegisterMessage.java
 * Author: ckj
 * Date: 2024/7/21 下午10:12
 * Description:
 * 用于用户注册的消息
 * History:
 * Date          Author   Version  Desc
 * 2024-01-01    bosssoft  1.0.0   initialize this file
 */
package com.bosssoft.message;

import com.bosssoft.constants.MessageTypeConstant;
import com.bosssoft.utils.ReaderUtil;

import java.io.BufferedReader;
import java.util.ArrayList;


/**
 * @author ckj
 */
public class RegisterMessage extends BaseMessage{
    private String userName;

    public RegisterMessage() {
        //null
    }

    @Override
    public BaseMessage parse(BufferedReader reader, Integer type) throws Exception {
        setUserName(ReaderUtil.readLineWithAes(reader));
        processContent(reader);
        return this;
    }

    /**
     *
     * @author ckj
     * @date 2024/07/23 上午9:34
     * 输入的 BufferedReader 中按照一定的规则读取多行内容，并将其存储在一个 ArrayList 中。
     * @param reader
     * @return void
     */
    private void processContent(BufferedReader reader) throws Exception {
        int contentLength = Integer.parseInt(ReaderUtil.readLineWithAes(reader));
        setContent(new ArrayList<>());
        for (int i = 0; i < contentLength; i++) {
            getContent().add(ReaderUtil.readLineWithAes(reader));
        }
        ReaderUtil.readLineWithAes(reader);
    }

    /**
     *
     * @author ckj
     * @date 2024/07/22 上午8:54
     * 当前用户注册
     * @return com.bosssoft.message.RegisterMessage
     */
    public RegisterMessage sendMessageBuild(String userName) {
        this.setUserName(userName);
        ArrayList<String> header = new ArrayList<>();
        header.add(userName);
        this.setHeader(header);
        this.setContent(new ArrayList<>());
        this.setType(MessageTypeConstant.REGISTERED_MESSAGE);
        return this;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}